<template>
  <el-row :gutter="20" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('newVisitis')">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="peoples" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            我的学生
          </div>
          <count-to :start-val="0" :end-val="memberBuyCourseTotal" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('messages')">
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="course" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            我的课程
          </div>
          <count-to :start-val="0" :end-val="courseTotal" :duration="3000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('purchases')">
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            历史总收益(元)
          </div>
          <count-to :start-val="0" :end-val="incomeAmount" :duration="3200" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('shoppings')">
        <div class="card-panel-icon-wrapper icon-shopping">
          <svg-icon icon-class="money2" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            当前可提现(元)
          </div>
          <count-to :start-val="0" :end-val="cashOutMoney" :duration="3600" class="card-panel-num" />
        </div>
        <div class="card-panel-description" style="margin-right: 60px;">
          <el-tooltip class="item" effect="dark" content="温馨提示：满400元可提现，每月只可提现一次" placement="top-start">
            <el-button :disabled="cashOutMoney<400" type="success" size="medium" @click="apply">去提现</el-button>
          </el-tooltip>
        </div>
      </div>
    </el-col>
  </el-row>
  <com-dialog v-model="dialogVisible" :title="title">
    <info-write
      v-if="comName === 'InfoWrite'"
      :info="info"
      @close="toggleVisible"
      @success="success"
    />

    <apply
        v-if="comName === 'Apply'"
        :info="info"
        @close="toggleVisible"
        @success="success"
    />
  </com-dialog>
</template>

<script lang="ts">
import CountTo from '_c/CountTo/index.vue'
import { defineComponent, ref } from 'vue'
import { getIndexApi, getTeacherInfoApi } from '@/views/dashboard/api'
import { Message } from '_c/Message'
import { useExample } from '@/hooks/useExample'
import InfoWrite from './InfoWrite.vue'
import Apply from './Apply.vue'
export default defineComponent({
  name: 'PanelGroup',
  components: {
    CountTo,
    InfoWrite,
    Apply
  },
  emits: ['handleSetLineChartData'],
  setup(props, { emit }) {
    // 可提现金额
    const cashOutMoney = ref<number>(0)
    // 累计收入
    const incomeAmount = ref<number>(0)
    // 我的课程
    const courseTotal = ref<number>(0)
    // 我的学生
    const memberBuyCourseTotal = ref<number>(0)
    const info = ref<any>(null)
    const {
      comName,
      toggleVisible,
      title,
      dialogVisible
    } = useExample()

    async function getDet() {
      try {
        const res = await getIndexApi()
        console.info('res:', res)
        if (res.code === 0) {
          cashOutMoney.value = res.data.cashOutMoney
          incomeAmount.value = res.data.incomeAmount
          courseTotal.value = res.data.courseTotal
          memberBuyCourseTotal.value = res.data.memberBuyCourseTotal
        }
      } catch (e) {
        console.log(e)
      }
    }
    getDet()

    // 讲师申请提现
    async function apply() {
      try {
        const res = await getTeacherInfoApi()
        if (res.code === 0) {
          if (res.data.payeeName === null || res.data.cardNumber === null || res.data.bank === null) {
            console.info('能进入到这里吗')
            open(res.data, 'InfoWrite')
          } else {
            open(res.data, 'Apply')
          }
        }
      } catch (e) {
        console.log(e)
      }
    }

    // 打开弹窗
    function open(row: any, component: string) {
      comName.value = component
      title.value = !row ? '新增' : (component === 'Apply' ? '申请提现' : '绑定收款信息')
      info.value = row || null
      toggleVisible(true)
    }

    // 成功之后的回调
    function success(type: string) {
      toggleVisible()
    }

    function handleSetLineChartData(type: string): void {
      emit('handleSetLineChartData', type)
    }
    return {
      handleSetLineChartData,
      apply,
      success,
      cashOutMoney,
      incomeAmount,
      courseTotal,
      memberBuyCourseTotal,
      dialogVisible,
      comName,
      title,
      toggleVisible,
      info
    }
  }
})
</script>

<style lang="less" scoped>
.panel-group {
  .card-panel-col{
    margin-bottom: 20px;
  }
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);
    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }
      .icon-people {
         background: #40c9c6;
      }
      .icon-message {
        background: #36a3f7;
      }
      .icon-money {
        background: #f4516c;
      }
      .icon-shopping {
        background: #34bfa3
      }
    }
    .icon-people {
      color: #40c9c6;
    }
    .icon-message {
      color: #36a3f7;
    }
    .icon-money {
      color: #f4516c;
    }
    .icon-shopping {
      color: #34bfa3
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-icon {
      float: left;
      font-size: 48px;
    }
    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}
</style>
